#!/bin/bash
source "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/alias.sh"

source "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/conda_and_pip_settings.sh"
source "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/conda_run.sh"
source "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/install_numba.sh"
source "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/print_sys_info.sh"

# APP_INSTALL_NAME="cosyvoice"
APP_TARGET_DIR_NAME="cosyvoice"

export CURRENT_APP_PATH="$LMD_APPS_DIR/$APP_TARGET_DIR_NAME"
echo "App Dir: ${CURRENT_APP_PATH}"

cd "$CURRENT_APP_PATH"
conda --version

if [ -n "$LMD_LAUNCH_APP_MODULE" ]; then
  echo "LMD_LAUNCH_APP_MODULE $LMD_LAUNCH_APP_MODULE"
else 
  echo "set default LMD_LAUNCH_APP_MODULE: CosyVoice2-0.5B"
  LMD_LAUNCH_APP_MODULE="CosyVoice2-0.5B"
fi

if [ -z "$HF_ENDPOINT" ] && [ -n "$HF_MIRROR" ]; then
    export HF_ENDPOINT="$HF_MIRROR"
fi

export GRADIO_TEMP_DIR="$CURRENT_APP_PATH/temp"
CONDA_PREFIX="./venv"
source "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/install_kaldifst.sh"

PORT_PARAM=""
if [ -n "$GRADIO_SERVER_PORT" ]; then
  PORT_PARAM="--port $GRADIO_SERVER_PORT"
fi

ensure_numba_0_61_2

echo "load model: $LMD_LAUNCH_APP_MODULE"
conda run -p $CONDA_PREFIX --no-capture-output python webui.py $PORT_PARAM --model_dir "pretrained_models/${LMD_LAUNCH_APP_MODULE}"